/*
 * Copyright 2009 Andreas Borglin andreas.borglin@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package borge.mobile.app.jakoon;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;

import borge.mobile.app.jakoon.Main.ESwtExecutor;
/**
 * Form that is loaded to present an error.
 * @author Andreas Borglin
 */
public class ErrorForm {

    /* TODOs
     * Fix hardcoded aligning
     */

    private Composite parent;
    private Composite top;
    private Composite errorComp;
    private Composite commandComp;
    private ESwtExecutor executor;
    
    private Label title;
    private Label error;
    
    protected ErrorForm(ESwtExecutor exec, Composite parent) {

        executor = exec;
        this.parent = parent;
        
        top = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        top.setLayout(layout);
        
        errorComp = new Composite(top, SWT.NONE);
        layout = new GridLayout();
        layout.numColumns = 1;
        layout.marginLeft = 210;
        errorComp.setLayout(layout);
        
        commandComp = new Composite(top, SWT.NONE);
        layout = new GridLayout();
        layout.numColumns = 2;
        layout.marginLeft = 445;
        commandComp.setLayout(layout);
        
        title = new Label(errorComp, SWT.NONE);
        title.setText("An error has occured");
        GridData data = new GridData();
        data.horizontalAlignment = GridData.FILL;
        data.widthHint = 300;
        title.setLayoutData(data);
        
        error = new Label(errorComp, SWT.WRAP);
        error.setText("Unknown");
        data = new GridData();
        data.horizontalAlignment = GridData.FILL;
        data.widthHint = 300;
        error.setLayoutData(data);

        
        Button startover = new Button(commandComp, SWT.PUSH);
        startover.setText("Start over");
        startover.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent event) {}
            public void widgetSelected(SelectionEvent event) {
                top.setVisible(false);
                executor.loadImageForm(true);
            }
        });

        Button exit = new Button(commandComp, SWT.PUSH);
        exit.setText("Exit");
        exit.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent event) {}
            public void widgetSelected(SelectionEvent event) {
                top.setVisible(false);
                executor.exit();
            }
        });
        
        errorComp.layout();
        commandComp.layout();
        top.layout();
    }
    
    protected void setVisible(boolean visible){
        top.setVisible(visible);
    }
    
    protected void setError(String errMsg){
        error.setText(errMsg);
    }
    

}
